import 'package:flutter/material.dart';

import 'package:date_format/date_format.dart';
import 'package:flutter_cupertino_date_picker/flutter_cupertino_date_picker.dart';

class DataTimePluginPage extends StatefulWidget {
  DataTimePluginPage({Key key}) : super(key: key);
  @override
  _DataTimePluginPageState createState() => _DataTimePluginPageState();
}

class _DataTimePluginPageState extends State<DataTimePluginPage> {

  bool _showTitle = true;

  DateTime _dateTime = DateTime.now();
  @override
  void initState() {

  }
  //Future 类似于 js promise
 void _showDatePicker() {
    DatePicker.showDatePicker(
      context,
      pickerTheme: DateTimePickerTheme(
        showTitle: _showTitle,
        confirm: Text('确人', style: TextStyle(color: Colors.red)),
        cancel: Text('取消', style: TextStyle(color: Colors.cyan)),
      ),
      minDateTime: DateTime.parse("2010-05-12"),
      maxDateTime: DateTime.parse("2021-11-25"),
      initialDateTime: DateTime.parse("2019-05-17"),
      dateFormat: "yyyy-MMMM-dd",
      locale: DateTimePickerLocale.zh_cn,
      onClose: () => print("----- onClose -----"),
      onCancel: () => print('onCancel'),
      onChange: (dateTime, List<int> index) {
        setState(() {
          _dateTime = dateTime;
        });
      },
      onConfirm: (dateTime, List<int> index) {
        setState(() {
          _dateTime = dateTime;
          print(dateTime);
        });
      },
    );
  }

  _showTimePick() async {
     TimeOfDay result =await  showTimePicker(
          context: context,
          initialTime: TimeOfDay(hour:12,minute: 30 )
     );
     print(result);


  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("日期时间"),
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: <Widget>[
            InkWell(
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Text("2019-21-12"),
                  Icon(Icons.arrow_drop_down)
                ],
              ),
              onTap: _showDatePicker,
            ),
            InkWell(
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Text("12:30"),
                  Icon(Icons.arrow_drop_down)
                ],
              ),
              onTap: _showTimePick,
            )
        ],
      ),
    );
  }
}
